package cn.buk.tms.hotel.service;

import cn.buk.common.util.DateUtil;
import cn.buk.tms.hotel.dto.HotelCity;
import cn.buk.tms.hotel.dto.HotelSearchResult;
import cn.buk.tms.hotel.dto.RoomPriceDto;
import cn.buk.tms.hotel.sc.HotelSearchCriteria;
import cn.buk.tms.hotel.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yfdai
 */
@Component
public class HotelServiceImpl implements HotelService {

  /**
   * 调用酒店接口的接口地址
   */
  private final String apiUrl;

  @Autowired
  public HotelServiceImpl(@Value("${api_hotel_url}") String apiUrl) {
    this.apiUrl = apiUrl;
  }

  @Override
  public HotelSearchResult searchHotel(HotelSearchCriteria sc) {

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("sc.pageNo", sc.getPage().getPageNo() + ""));
    params.add(new BasicNameValuePair("sc.pageSize", sc.getPage().getPageSize() + ""));
    params.add(new BasicNameValuePair("sc.cityId", sc.getCityId() + ""));
    params.add(new BasicNameValuePair("sc.checkInDate", DateUtil.formatDate(sc.getCheckInDate(), "yyyy-MM-dd")));
    params.add(new BasicNameValuePair("sc.checkOutDate", DateUtil.formatDate(sc.getCheckOutDate(), "yyyy-MM-dd")));
    params.add(new BasicNameValuePair("sc.roomCount", sc.getRoomCount() + ""));
    params.add(new BasicNameValuePair("sc.guestCount", sc.getGuestCount() + ""));
    params.add(new BasicNameValuePair("sc.hotelStar", sc.getHotelStar() + ""));
    params.add(new BasicNameValuePair("sc.hotelName", sc.getHotelName()));
    params.add(new BasicNameValuePair("sc.district", sc.getDistrict()));
    params.add(new BasicNameValuePair("sc.businessDistrict", sc.getBusinessDistrict()));
    params.add(new BasicNameValuePair("sc.hotelType", sc.getHotelType() + ""));
    final String url = apiUrl + "/searchHotel?";
    final String jsonStr = HttpUtil.postUrl(url, params);

    HotelSearchResult rs = JSON.parseObject(jsonStr, HotelSearchResult.class);

    return rs;
  }

  @Override
  public List<RoomPriceDto> searchRoomPrice(int roomId, Date checkInDate, Date checkOutDate) {
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("roomId", roomId + ""));
    params.add(new BasicNameValuePair("checkInDate", DateUtil.formatDate(checkInDate, "yyyy-MM-dd")));
    params.add(new BasicNameValuePair("checkOutDate", DateUtil.formatDate(checkOutDate, "yyyy-MM-dd")));

    final String url = apiUrl + "/searchRoomPrices?";
    final String jsonStr = HttpUtil.postUrl(url, params);

    List<RoomPriceDto> rs = JSON.parseArray(jsonStr, RoomPriceDto.class);

    return rs;
  }

  @Override
  public List<HotelCity> searchHotelCities() {
    final String url = apiUrl + "/searchCity?";
    final String jsonStr = HttpUtil.getUrl(url, null);

    List<HotelCity> rs = JSON.parseArray(jsonStr, HotelCity.class);

    return rs;
  }
}
